// pages/home/index.js
const { formatTime, formatDate } = require("../../utils/util");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputValue: '',
    statusMap: {
      0: '报名中',
      1: '已成团',
      2: '已结束'
    },
    list: []
  },

  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  bindKeyConfirm: function () {
    this.fetchActivityList(this.data.inputValue);
  },

  onItemClick: function (e) {
    let id = e.currentTarget.dataset.value;
    wx.navigateTo({
      url: `detail/index?id=${id}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  fetchActivityList: function (keyword = '') {
    const that = this
    wx.cloud.callContainer({
      config: {
        env: "prod-9gno6bd407ac56da"
      },
      path: "/api/activity/list",
      header: {
        "X-WX-SERVICE": "springboot-kd4g"
      },
      method: "POST",
      data: {
        keyword
      },
      success(res) {
        let acList = res?.data?.data || [];
        let tmpList = []
        if (acList.length > 0) {
          acList.forEach((item) => {
            let tmpItem = item
            if (tmpItem.activityTime) {
              tmpItem.activityTime = formatTime(new Date(tmpItem.activityTime))
            }
            if (tmpItem.createTime) {
              tmpItem.createTime = formatDate(new Date(tmpItem.createTime))
            }
            tmpList.push(tmpItem)
          })
        }

        that.setData({
          list: tmpList
        })
      },
      fail(res) {
        console.log('fail==', res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.fetchActivityList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})